//
//  MyHourMinutePickerView.m
//  datePickerView
//
//  Created by lianchuang on 14-11-21.
//  Copyright (c) 2014年 lianchuang. All rights reserved.
//

#import "MyHourMinutePickerView.h"

@interface MyHourMinutePickerView () <UIPickerViewDataSource, UIPickerViewDelegate>


@end

@implementation MyHourMinutePickerView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self makeHourAndMinutePicker];
    }
    return self;
}
-(void)makeHourAndMinutePicker
{
    self.backgroundColor = [UIColor whiteColor];

    
    self.cancelButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    self.cancelButton.frame = CGRectMake(15, 10, 50, 30);
    [self.cancelButton setTitle:@"取消" forState:UIControlStateNormal];
    [self addSubview:self.cancelButton];
    [self.cancelButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    
    self.sureButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    self.sureButton.frame = CGRectMake(self.width-15-50,10, 50, 30);
    [self.sureButton setTitle:@"确定" forState:UIControlStateNormal];
    [self.sureButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [self addSubview:self.sureButton];
    
    //创建pickerView
    self.pickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 40, self.frame.size.width, self.frame.size.height - 40)];
    self.pickerView.delegate = self;
    self.pickerView.dataSource = self;
    self.pickerView.showsSelectionIndicator = YES;
    [self addSubview:self.pickerView];
    
    //初始化年月日
    NSDate *nowDate = nil;
    if (self.defaultDate) {
        nowDate = self.defaultDate;
        
    } else {
        nowDate = [NSDate date];
        
    }
    NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
    [formatter setDateFormat:@"yyyy-MM-dd-HH-mm"];
    NSString *dateStr = [formatter stringFromDate:nowDate];
    NSArray *dateArray = [dateStr componentsSeparatedByString:@"-"];
    _year = [dateArray[0] intValue];
    _month = [dateArray[1] intValue];
    _day = [dateArray[2] intValue];
    _hour = [dateArray[3] intValue];
    _minute = [dateArray[4] intValue];
    
    
    [self.pickerView selectRow:[self calculateMiddleIndexWithFirstIndex:self.hour circulateTimes:HOUR_CIRCULATE_TIMES elementCount:HOUR_COUNT] inComponent:0 animated:NO];
    [self.pickerView selectRow:[self calculateMiddleIndexWithFirstIndex:self.minute circulateTimes:MINUTE_CIRCULATE_TIMES elementCount:MINUTE_COUNT] inComponent:1 animated:NO];

}

- (NSInteger)calculateMiddleIndexWithFirstIndex:(NSInteger)firstIndex circulateTimes:(NSInteger)times elementCount:(NSInteger)count
{
    if (times % 2 != 0) {
        times--;
        
    }
    
    NSInteger index = firstIndex + times / 2 * count;
    
    return index;
    
}

#pragma mark - UIPickerViewDelegate and DataSource -

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    if (pickerView == self.pickerView) {
        return 2;
    }else
    {
        return 0;
    }
    
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    if (pickerView == self.pickerView)
    {
        switch (component)
        {
            case 0:
                return HOUR_CIRCULATE_TIMES * HOUR_COUNT;
                break;
                
            case 1:
                return MINUTE_CIRCULATE_TIMES * MINUTE_COUNT;
                break;
                
            default:
                return 0;
                break;
        }
        
    }
    else
    {
        return 0;
    }
}
- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component
{
    if (pickerView == self.pickerView) {
        return 90.0;
    }else
    {
        return 0;
    }
    
}
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
{
    if (pickerView == self.pickerView) {
        UILabel *label = nil;
        label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 58, 35)];
        label.font = [UIFont boldSystemFontOfSize:13.f];
        label.backgroundColor = [UIColor clearColor];
        label.textAlignment = NSTextAlignmentCenter;
        switch (component) {
            case 0:
                label.text = [NSString stringWithFormat:@"%.2ld时", row % HOUR_COUNT];
                break;
                
            case 1:
                label.text = [NSString stringWithFormat:@"%.2ld分", row % MINUTE_COUNT];
                break;
                
            default:
                return nil;
                break;
        }
        return label;
    }else
    {
        return nil;
    }
}


- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    if (pickerView == self.pickerView) {
     
        switch (component) {
            case 0:
                _hour = row % HOUR_COUNT;
                [pickerView selectRow:[self calculateMiddleIndexWithFirstIndex:self.hour circulateTimes:HOUR_CIRCULATE_TIMES elementCount:HOUR_COUNT] inComponent:0 animated:NO];
                
                break;
                
            case 1:
                _minute = row % MINUTE_COUNT;
                [pickerView selectRow:[self calculateMiddleIndexWithFirstIndex:self.minute circulateTimes:MINUTE_CIRCULATE_TIMES elementCount:MINUTE_COUNT] inComponent:1 animated:NO];
                
                break;
                
            default:
                break;
        }
    }

}
-(CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component
{
    if (pickerView == self.pickerView) {
        return 30;
    }else{
        return 0;
    }
}

-(NSString *)getDateString
{
    NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
    [formatter setDateFormat:HourMinute_DATE_FORMAT];
    NSDate *date = [self getDate];
    return [formatter stringFromDate:date];    
}
-(NSDate *)getDate
{
    NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
    [formatter setDateFormat:@"yyyy-MM-dd-HH-mm"];
    NSDate *date = [formatter dateFromString:[NSString stringWithFormat:@"%ld-%ld-%ld-%ld-%ld", self.year, self.month, self.day, self.hour, self.minute]];
    return date;
    
}

@end
